package unwdmi.listener;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import unwdmi.connector.ClientConnector;
import unwdmi.connector.WeatherStationConnector;
/**
 * Listens for clients that try to connect with the server
 * @author Stephan E G Veenstra
 *
 */
public class ClientListener implements Runnable {

	private ServerSocket serverSocket;
	private boolean listen;
	
	public ClientListener(ServerSocket serverSocket){
		this.serverSocket = serverSocket;
		this.listen = true;
	}
	
	public void run() {
		System.out.println("Listening for Clients to connect...");
		Socket clientSocket;
		while(this.listen){
			try {
				// Listen for connecting WeatherStations
				clientSocket = serverSocket.accept();
				// Start a new thread for the connection
				Thread t = new Thread(new ClientConnector(clientSocket));
				t.start();
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
